/*
	jGrouse framework
	Copyright 2007 by Denis Riabtchik 
	All rights reserved.
	
	Licensed under modified BSD license. See license.txt for details
*/

/**
	Module with PageContainer component
	@module jgrouse.widgets.PageContainer
	@requires jgrouse.widgets.Container
	@requires jgrouse.widgets.SimpleLayout
*/
jgrouse.module({
	name : 'jgrouse.widgets.PageContainer',
	requires : ['jgrouse.widgets.Container', 'jgrouse.widgets.SimpleLayout'],
	imports : ['jgrouse.widgets.TemplateParser', 'jgrouse.dom.Event', 'jgrouse.dom.Element', 'jgrouse.string.*'],  
	body : function(embed)
	{
		eval(embed);
		
		jgrouse.widgets.registerWidget('pageContainer', 'jgrouse.widgets.PageContainer');
		
		/**
			PageContainer is a container widget that is capable of loading its content from external URL.
			The content of loaded file would be incapsulated into a container that would be inserted into given
			PageContainer.
			The external file must contain a tag with id="content". Only contents of that tag would be used.
			If the file contains a script tag with id="init", then that code should contain initialization block for 
			the loaded file. The code must pass control to function with name <code>callback</code> once the initialization is complete.
			The framework would supply the definition and implementation of that function.<br>
			Example:<br>
			<pre>
				&lt;script id="init"&gt;
					jgrouse.require('jgrouse.widgets.StaticText', callback);
				&lt;/script id="init"&gt;
			</pre>
			Exposed properties:
			<ul>
				<li> contentUrl - setting that property would trigger reloading of the contents of the container.
				If the property is set to null or empty string, then container would use contents from original
				template</li>
			</ul>
			@class jgrouse.widgets.PageContainer
			@extends jgrouse.widgets.Container
			 
		*/
		jgrouse.define('jgrouse.widgets.PageContainer', 'jgrouse.widgets.Container', function(salt)
		{
			eval(salt);
			
			return {
				/**
					@constructor initialize
					@inheritdesc
				*/
				initialize : function(options, styles)
				{
					_sup(this, arguments);
					/**
						Current URL for the content
						@variable {String} _contentUrl
					*/
					this._contentUrl = undefined;
					this._loader = jgrouse.bind(this, this._postLoad);
					/**
						Original HTML that appeared in template when PageContainer was created
						@variable {String} _originalHTML
					*/
					this._originalHTML = null;
					/**
						Container with current content
						@variable {jgrouse.widget.Container} _currentContainer
					*/
					this._currentContainer = null;
				},
				
				/**
					Set content URL for the container
					@function setContentUrl
					@param {String} url - url to be used to load new content. When empty, the component
					would use original HTML
				*/
				setContentUrl : function(url)
				{
					var ancorName = null;
					if (url)
					{
						var ancor = unescape(url);
						var a = ancor.split('#');
						ancor = a[0];
						ancorName = a[1];
						if (ancor)
						{
							if (this._contentUrl != ancor)
							{
								this._contentUrl = ancor;
								var athis = this;
								jgrouse.ClassLoader.instance.getText(ancor + '?__jgdummy=1', function(text)
								{
									athis._postLoad(text, ancorName);
								});
							}
							else
							{
								this._scrollToAncor(ancorName);
							}
						}
						else
						{
							this._scrollToAncor(ancorName);
						}
						
					}
					else
					{
						if (this._contentUrl || typeof this._contentUrl == 'undefined')
						{
							this._contentUrl = null;
							this._postLoad(this._originalHTML)
						}
						else
						{
							this._contentUrl = null;
						}
					}
				},
				
				render : function()
				{
					if (!this._contentUrl)
					{
						this.setContentUrl(null);
					}
					_sup(this, arguments);
				},
				
				/**
					Create layout for given page container. This container would be using SimpleLayout and remember
					what was the original content of passed template
					@function createLayout
					@param {template} template - template to be used
					@see jgrouse.widgets.Container.createLayout
					
				*/
				createLayout : function(template)
				{
					var simpleLayout = new jgrouse.widgets.SimpleLayout();
					this._originalHTML = null;
					if (typeof template == 'string')
					{
						this._originalHTML = template;
					}
					else 
					{
						var dom = null;
						if (template instanceof jgrouse.widgets.TemplateParser)
						{
							dom = template._currentNode;
						}
						else
						{
							dom = template;
						}
						this._originalHTML = dom.innerHTML;
						dom.innerHTML = '';
					}
					return simpleLayout;
				},				
				
				_scrollToAncor : function(ancorName)
				{
					if (ancorName)
					{
						var ae = document.getElementsByName(ancorName)[0];
						if (ae)
						{
							Element.scrollIntoView(ae, 'left');
						}
					}
					else
					{
						if (this._domElement && this._domElement.firstChild)
						{
							Element.scrollIntoView(this._domElement.firstChild, 'left');
						}
					}
					
				},
								
				_postLoad : function(text, ancorName)
				{
					var insertText = (typeof text == 'string')? text : text.responseText;
					var div = document.createElement('div');
					div.innerHTML = insertText;
					var root = null;
					var initScript = null;
					
					jgrouse.dom.Element.walkTree(div, function(node)
					{
						if (node.id == 'content')
						{
							root = node;
						}
						if (node.tagName && node.tagName.toLowerCase() == 'script' && node.id == 'init')
						{
							initScript = node;
						}
					});

					if (!root)
					{
						root = div;
					}
					else
					{
						root.parentNode.removeChild(root);
						jgrouse.dom.Element.disposeDom(div);
					}

					var athis = this;
					
					function doProcess()
					{					
						if (athis._currentContainer)
						{
							athis.removeWidget(athis._currentContainer, true);
							athis._currentContainer = null;
						}						
						var container = new jgrouse.widgets.Container();;
						athis._currentContainer = container;
						container.setTemplate(root);
						//container.bind(athis._mvcDriver);
						athis.addWidget(container);
						athis._scrollToAncor(ancorName);
					}
					
					if (initScript)
					{
						var s = 'var initFunc = function(callback){' + initScript.innerHTML + '}';
						eval(s);
						initFunc(doProcess);
					}
					else
					{
						doProcess();
					}
					
				},
				
				/**
					@function dispose
					@inheritdesc
				*/				
				dispose : function()
				{
					if (this._currentContainer)
					{
						this._currentContainer.remove(true);
					}
					_sup(this, arguments);
				}
			}
		});			
	}

});